import time
import base64
import random


class Authorization(object):
    def __init__(self):
        pass

    # 根据手机、随机数、时间2小时过期 生成token
    def get_token(self, username):
        token = base64.b64encode((":".join([str(username),
                                            str(random.random()),
                                            str(time.time() + 7200)])).encode()).decode()
        # print(time.time())
        return token

    def vertify_token(self, token):
        info = base64.b64decode(token.encode()).decode()
        fail_time = info.split(':')[2]
        # print(time.time())
        # print(fail_time)
        if str(time.time()) > fail_time:
            return False
        else:
            return True
